package com.jz.community.moduleothers.scan.ui;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxImageTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleothers.R;
import com.jz.community.moduleothers.R2;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class QRShowActivity extends BaseMvpActivity {

    @BindView(R2.id.rl_qr_show_warning)
    RelativeLayout rlQrShowWarning;
    @BindView(R2.id.iv_qr_show_qr)
    ImageView ivQrShowQr;
    @BindView(R2.id.rl_qr_show_qr)
    RelativeLayout rlQrShowQr;
    private String qr = "";
    private int ivBarWidth;

    @Override
    protected int getContentView() {
        return R.layout.module_others_activity_qr_show;
    }

    @Override
    protected void initView() {
        qr = getIntent().getStringExtra("qr");
        if (!Preconditions.isNullOrEmpty(qr)) {
            setQr();
        }
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    protected void addListener() {
        super.addListener();
        rlQrShowWarning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rlQrShowWarning.setVisibility(View.GONE);
                rlQrShowQr.setVisibility(View.VISIBLE);
            }
        });

        rlQrShowQr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @SuppressLint("CheckResult")
    private void setQr() {
        ivBarWidth = SHelper.getScrrenWidth(this) - RxImageTool.dp2px(80);
        Observable.just(qr)
                .map(new Function<String, Bitmap>() {
                    @Override
                    public Bitmap apply(String s) throws Exception {
                        return QRCodeEncoder.syncEncodeBarcode(s, ivBarWidth, RxImageTool.dp2px(130), SHelper.dp2px(QRShowActivity.this, 20));
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        ivQrShowQr.setImageBitmap(bitmap);
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
